package tool

import (
	"html/template"
	"net/http"

	"github.com/gin-gonic/gin"
)

// RedocHandler 返回 Redoc 界面的处理器
func RedocHandler() gin.HandlerFunc {
	return func(c *gin.Context) {
		// 使用模板来生成 Redoc 页面
		const redocHTML = `<!DOCTYPE html>
<html>
  <head>
    <title>API 文档</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="https://fonts.googleapis.com/css?family=Montserrat:300,400,700|Roboto:300,400,700" rel="stylesheet">
    <style>
      body {
        margin: 0;
        padding: 0;
        font-family: 'Roboto', sans-serif;
      }
      .redoc-wrap {
        background-color: #FFFFFF;
      }
      .menu-content {
        background-color: #F5F7F9;
      }
      img {
        max-width: 100%;
      }
    </style>
  </head>
  <body>
    <redoc 
      spec-url='/swagger/doc.json'
      hide-download-button="true"
      expand-responses="200,201"
      path-in-middle-panel="true"
      scroll-y-offset="56"
    ></redoc>
    <script src="https://cdn.redoc.ly/redoc/latest/bundles/redoc.standalone.js"></script>
  </body>
</html>`

		tmpl, err := template.New("redoc").Parse(redocHTML)
		if err != nil {
			c.String(http.StatusInternalServerError, err.Error())
			return
		}

		c.Status(http.StatusOK)
		c.Header("Content-Type", "text/html; charset=utf-8")

		err = tmpl.Execute(c.Writer, nil)
		if err != nil {
			c.String(http.StatusInternalServerError, err.Error())
			return
		}
	}
}
